Meteorite Data Set contains information about where and when meteorites were found or fell. There is a difference between a ‘found’ meteorite and a ‘fell’ meteorite. Those that are classified as ‘fell’ are those which were seen falling from the sky and later tracked down successfully, whereas those which were ‘found’ do not have a exact date or location of their falling.

meteorites <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-06-11/meteorites.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   name = col_character(),
##   id = col_double(),
##   name_type = col_character(),
##   class = col_character(),
##   mass = col_double(),
##   fall = col_character(),
##   year = col_double(),
##   lat = col_double(),
##   long = col_double(),
##   geolocation = col_character()
## )
# start by summarising the count of meteorites per year
meteorites_total <- meteorites %>%
  group_by(year) %>%
  dplyr::summarise(Count = n())
# plot
plot(x = meteorites_total$year,
     y = meteorites_total$Count,
     type = "l")

# subset data for 1900 --> present
which.max(meteorites_total$year)
## [1] 265
meteorites_total[265,] # this is the year 2101? in the future... 
## # A tibble: 1 x 2
##    year Count
##   <dbl> <int>
## 1  2101     1
meteorites_total <- meteorites_total[-265, ]
which.max(meteorites_total$year)
## [1] 264
meteorites_total[264,] # the most recent is now 2013.
## # A tibble: 1 x 2
##    year Count
##   <dbl> <int>
## 1  2013    11
# subset data 1900-2013
meteorites_total <- meteorites_total %>%
  subset(year > 1900)
# plot again
plot(x = meteorites_total$year,
     y = meteorites_total$Count,
     type = "l")

# still not that much happening up until 1970 - lets subset again
meteorites_total <- meteorites_total %>%
  subset(year > 1970)
# plot again
plot(x = meteorites_total$year,
     y = meteorites_total$Count,
     type = "l")

# what about just fallen meteorites because those that were found may not have fallen in that year.
meteorites_total2 <- meteorites %>%
  filter(fall == "Fell") %>%
  group_by(year) %>%
  dplyr::summarise(Count = n())

plot(x = meteorites_total2$year,
     y = meteorites_total2$Count,
     type = "l")

# again not much going on until 1750? so lets subset down
meteorites_total2 <- meteorites_total2 %>%
  subset(year > 1750)

plot(x = meteorites_total2$year,
     y = meteorites_total2$Count,
     type = "l",
     col = "black",
     lwd = 2)

# group by decade
meteorites3 <- meteorites %>% 
  mutate(decade = year - year%%10) %>% 
  filter(decade>=1750, decade<=2005) %>% 
  select(name,mass,fall, year,decade, lat,long) %>% 
  drop_na()

# filter again to just fallen meteorites
meteorites_total3 <- meteorites3 %>%
  filter(fall == "Fell") %>%
  group_by(decade) %>%
  dplyr::summarise(Count = n())
# Downloaded title font from (https://www.dafont.com/galaxy-1.font)

# make objects containing text
subtitle <- "Fallen meteorites are those that were spotted falling and later located"

section1 <- "The idea of\nmeteorites was\nlargely rejected\nbefore the 1790s"

section2 <- "Scientific knowlegde of meteorites increased\nduring the 19th century, as well as the world\npopulation and education levels."

section3 <- "The number of meteorites has\nrecently stablised, there may\nalso be a time lag between a\nmeteorite falling and its\nappearance in records."

png(filename = "meteorites01.png", width = 30, height = 20, units = "cm", res = 300)

# plot
par(bg = "white", xpd = NA)

plot(x = meteorites_total3$decade,
     y = meteorites_total3$Count,
     type = "l",
     col = "grey30",
     lwd = 4,
     xlab = "",
     ylab = "",
     bty = "n",
     xaxt = "none", yaxt = "none",
     cex.lab = 0.8, 
     col.lab = "grey50")

# add title
title("The Rise of Fallen Meteorites",
      family = "Galaxy 1", 
      cex.main = 3,
      col.main = "grey20")

# add y axis title
title(ylab = "No. of observed falls (av. over 10 year period)",
      line = 1.2, 
      col.lab = "grey50",
      cex.lab = 0.8,
      family = "Courier")

title(xlab = "Year",
      line = 1.5, 
      col.lab = "grey50",
      cex.lab = 0.8,
      family = "Courier")

# add subtitle
text(1915, 92, subtitle,
     col = "grey30",
     family = "Courier")

# x axis
axis(1, seq(1750, 2000, 10),
     labels = F,
     col = "grey50",
     col.axis = "grey50",
     tck = -0.005)

text(x = seq(1750, 2000, 10),
     y = par("usr")[3] - 2,
     labels = meteorites_total3$decade,
     xpd = NA, srt = 35,
     adj = 0.9, cex = 0.8,
     col = "grey50",
     family = "Courier")

# y axis
axis(2, seq(0, 90, 10),
     las = 2,
     col = "grey50",
     col.axis = "grey50",
     cex.axis = 0.8,
     tck = -0.01,
     line = -0.8,
     family = "Courier")

# add rectangle for first section
rect(1750, 0, 1785, 90,
     col = alpha("#99D6EA", 0.5),
     border = "white")
# add text to first section
text(1767, 15, section1,
     col = "grey30",
     cex = 0.8,
     family = "Courier")

# add rect for second section
rect(1786, 0, 1930, 90,
     col = alpha("#FDD85D", 0.5),
     border = "white")
# add text to second section
text(1850, 80, section2,
     col = "grey30",
     cex = 0.9,
     family = "Courier")

# add rect for third section
rect(1931, 0, 2000, 90,
     col = alpha("#6798C0", 0.5),
     border = "white")
# add text to third section
text(1970, 80, section3,
     col = "grey30",
     cex = 0.8,
     family = "Courier")

# add lines for important events
segments(1794, 0, 1794, 12,
      col = "coral",
      lwd = 2)
segments(1833, 0, 1833, 23,
      col = "coral",
      lwd = 2)

# add text for important events
text(1812, 5, "1794: Ernst Chladni,\n'father of meteoritics'\nhypothesised that\nmeteorites are rocks\nfrom space",
     col = "coral",
     family = "Courier",
     cex = 0.6)

text(1855, 10,
     "1833: Leonid Shower,\nsparked modern day\nawareness of meteorites\nand after this point\nscientific studies began",
     col = "coral",
     family = "Courier",
     cex = 0.6)

lines(x = meteorites_total3$decade,
     y = meteorites_total3$Count,
     type = "l",
     col = "grey30",
     lwd = 4,
     col.lab = "grey50")

# add points
points(x = meteorites_total3$decade,
       y = meteorites_total3$Count,
       pch = 8, 
       cex = 1.5,
       col = "#6798C0")

dev.off()
## quartz_off_screen 
##                 2

Now for a plot using ggplot

# order the meteorites by their mass
meteorites_ordered <- meteorites[order(-meteorites$mass), ]

# find the top say 20 largest? and subset
top20 <- meteorites_ordered[1:20,]
# Convert mass from g to tonnes
top20$mass <- (top20$mass/1000000)

# plot mass as a bar chart, reordering names by mass
ggplot(top20,
       aes(x = reorder(name, mass),
           y = mass,
           fill = class)) +
  geom_bar(stat = "identity") +
  coord_flip() + # flip axis so names down the side
  labs(y = "Mass (tonnes)",
       x = "")

# get the countries for each of these top 20 meteorites.
country <- map.where(database = "world",
                     top20$long, # use lat and long values from dataset
                     top20$lat)

country # entry 2 and 16 are NAs
##  [1] "Namibia"   NA          "Argentina" "USA"       "China"     "Namibia"  
##  [7] "Mexico"    "Australia" "Russia"    "Mexico"    "Tanzania"  "USA"      
## [13] "Mexico"    "China"     "Australia" NA          "Brazil"    "USA"      
## [19] "China"     "Chile"
top20$geolocation[2] # Greenland
## [1] "(76.13333, -64.93333)"
country[2] <- "Greenland"
top20$geolocation[16] # Brazil
## [1] "(-26.21667, -48.6)"
country[16] <- "Brazil"

# use country code to convert country name into iso2 code ready to use with ggflag
country_code <- countrycode(country, 
            "country.name",
            "iso2c")

# change to lower case
country_code <- tolower(country_code)

# bind country code with top 20 
top20c <- cbind(top20, country_code)

# combine some classes
top20c$class <- factor(top20c$class)
levels(top20c$class)
##  [1] "H5"                "Iron, IAB-MG"      "Iron, IAB-ung"    
##  [4] "Iron, IC"          "Iron, IIAB"        "Iron, IIIAB"      
##  [7] "Iron, IIIE"        "Iron, IVA"         "Iron, IVB"        
## [10] "Iron, ungrouped"   "Mesosiderite-A1"   "Pallasite, PMG-an"
levels(top20c$class) <- c("Stony Chondrites",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Iron (Metallic)",
                          "Stony-Iron",
                          "Stony-Iron")
top20c$class <- factor(top20c$class, levels = c("Iron (Metallic)", "Stony-Iron", "Stony Chondrites")) 

# create new column combining name of meteorite and year it fell
top20c$title <- paste(top20c[ , 1],",", top20c[ , 7])

# now replot including flags
meteorites02 <- ggplot(top20c,
       aes(x = reorder(title, mass), y = mass, fill = class)) +
  geom_segment(aes(x = reorder(title, mass), xend = title, 
                   y = 0, yend = mass,
                   color = class),
               size = 3, alpha = 0.2) +
  geom_segment(aes(x = reorder(title, mass), xend = title, 
                   y = 0, yend = mass,
                   color = class),
               size = 1, alpha = 0.8) +
  geom_flag(aes(x = reorder(title, mass), y = 0, 
                country = country_code), 
            size = 6) + 
  geom_label(label = round(top20$mass, 0),
             nudge_y = 1,
             colour = "white",
             family = "Avenir",
             show.legend = F) +
  geom_label(label = "Mass in tonnes",
             x = 8.5, y = 48,
             colour = "white",
             fill = "grey60",
             family = "Avenir") +
  scale_color_manual(values = c("#8B8682", "coral", "#ffcc00")) +
  scale_fill_manual(values = c("#8B8682",  "coral", "#ffcc00")) +
  coord_flip(clip = "off") +
  labs(title = "Name of Meteorite,   Year                     Country                                                                                                                                                                                                                                    Mass in Tonnes",
       y = "",
       x = "") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(hjust = 0,
                               colour = "#615d5b",
                               family = "Avenir", 
                               size = 10),
    plot.background = element_rect(fill = "white"),
    plot.title = element_text(colour = "#8B8682",
                              family = "Avenir",
                              size = 8,
                              vjust = 0,
                              hjust = 1),
    panel.grid = element_blank(),
    plot.margin = unit(c(1, 1, 1, 1), "cm"),
    legend.title = element_blank(),
    legend.position = c(0.77, 0.33),
    axis.title.x = element_text(colour = "#615d5b",
                                family = "Avenir",
                                size = 10),
    axis.text.x = element_blank(),
    axis.line.x = element_blank()
  ) +
  annotate("text",
           x = 12, 
           y = 48, 
           label = "The largest meteorites found\non earth are made of Iron",
           color = "#615d5b",
           family = "Avenir",
           size = 7) +
  annotate("text",
           x = 10, 
           y = 48,
           label = "18 out of 20 of the largest meteorites that\nhave been found or fallen were made of Iron.",
           colour = "#8B8682",
           family = "Avenir",
           size = 4) 

# save file as meteorites02
ggsave(filename = "meteorites02.png", plot = meteorites02, 
       width = 28, height = 20, unit = "cm")

Interactive plot: where are the meteorites in the world?

library(leaflet)
library(forcats)
library(leaflet.extras)

meteorites$class <- factor(meteorites$class)
unique(meteorites$class)
##   [1] L5                         H6                        
##   [3] EH4                        Acapulcoite               
##   [5] L6                         LL3-6                     
##   [7] H5                         L                         
##   [9] Diogenite-pm               Unknown                   
##  [11] H4                         H                         
##  [13] Iron, IVA                  CR2-an                    
##  [15] LL5                        CI1                       
##  [17] L/LL4                      Eucrite-mmict             
##  [19] CV3                        Ureilite-an               
##  [21] Stone-uncl                 L3                        
##  [23] Angrite                    LL6                       
##  [25] L4                         Aubrite                   
##  [27] Iron, IIAB                 Iron, IAB-sLL             
##  [29] Iron, ungrouped            CM2                       
##  [31] OC                         Mesosiderite-A1           
##  [33] LL4                        C2-ung                    
##  [35] LL3.8                      Howardite                 
##  [37] Eucrite-pmict              Diogenite                 
##  [39] LL3.15                     LL3.9                     
##  [41] Iron, IAB-MG               H/L3.9                    
##  [43] Iron?                      Eucrite                   
##  [45] H4-an                      L/LL6                     
##  [47] Iron, IIIAB                H/L4                      
##  [49] H4-5                       L3.7                      
##  [51] LL3.4                      Martian (chassignite)     
##  [53] EL6                        H3.8                      
##  [55] H3-5                       H5-6                      
##  [57] Mesosiderite               H5-7                      
##  [59] L3-6                       H4-6                      
##  [61] Ureilite                   Iron, IID                 
##  [63] Mesosiderite-A3/4          CO3.3                     
##  [65] H3                         EH3/4-an                  
##  [67] Iron, IIE                  L/LL5                     
##  [69] H3.7                       CBa                       
##  [71] H4/5                       H3/4                      
##  [73] H?                         H3-6                      
##  [75] L3.4                       Iron, IAB-sHL             
##  [77] L3.7-6                     EH7-an                    
##  [79] Iron                       CR2                       
##  [81] CO3.2                      K3                        
##  [83] L5/6                       CK4                       
##  [85] Iron, IIE-an               L3.6                      
##  [87] LL3.2                      Pallasite                 
##  [89] CO3.5                      Lodranite                 
##  [91] Mesosiderite-A3            L3-4                      
##  [93] H5/6                       Pallasite, PMG            
##  [95] Eucrite-cm                 L5-6                      
##  [97] CO3.6                      Martian (nakhlite)        
##  [99] LL3.6                      C3-ung                    
## [101] H3-4                       CO3.4                     
## [103] EH3                        Iron, IAB-ung             
## [105] Winonaite                  LL                        
## [107] Eucrite-br                 Iron, IIF                 
## [109] R3.8-6                     L4-6                      
## [111] EH5                        LL3.00                    
## [113] H3.4                       Martian (shergottite)     
## [115] Achondrite-ung             LL3.3                     
## [117] C                          H/L3.6                    
## [119] Iron, IIIAB-an             LL7                       
## [121] Mesosiderite-B2            LL4-6                     
## [123] CO3.7                      L/LL6-an                  
## [125] Iron, IAB complex          Pallasite, PMG-an         
## [127] H3.9/4                     L3.8                      
## [129] LL5-6                      LL3.8-6                   
## [131] L3.9                       L4-5                      
## [133] L3-5                       LL4/5                     
## [135] L4/5                       H3.9                      
## [137] H3.6-6                     H3.8-5                    
## [139] H3.8/4                     H3.9-5                    
## [141] CH3                        R3.8-5                    
## [143] L3.9/4                     E4                        
## [145] CO3                        Chondrite-ung             
## [147] H~5                        H~6                       
## [149] L/LL3.10                   EL5                       
## [151] LL3                        L~6                       
## [153] L~3                        H~4                       
## [155] L(LL)3.5-3.7               Iron, IIIE-an             
## [157] H3.6                       L3.4-3.7                  
## [159] L3.5                       CM1/2                     
## [161] Martian (OPX)              Brachinite                
## [163] LL7(?)                     LL6(?)                    
## [165] Eucrite-Mg rich            H3.5-4                    
## [167] EL3                        R3.6                      
## [169] H3.5                       CM1                       
## [171] L/LL3                      H7                        
## [173] L(?)3                      L3.2                      
## [175] L3.7-3.9                   Mesosiderite-B1           
## [177] Eucrite-unbr               LL3.7                     
## [179] CO3.0                      LL3.5                     
## [181] L3.7-4                     CV3-an                    
## [183] Lunar (anorth)             L3.3                      
## [185] Iron, IAB-sLM              Lunar                     
## [187] Iron, IC                   Iron, IID-an              
## [189] Iron, IIIE                 Iron, IVA-an              
## [191] CK6                        L3.1                      
## [193] CK5                        H3.3                      
## [195] H3.7-6                     E6                        
## [197] H3.0                       H3.1                      
## [199] L3.0                       L/LL3.4                   
## [201] C6                         LL3.0                     
## [203] Lunar (gabbro)             R4                        
## [205] C4                         Iron, IIG                 
## [207] Iron, IIC                  C1-ung                    
## [209] H5-an                      EH4/5                     
## [211] Iron, IIIF                 R3-6                      
## [213] Mesosiderite-B4            L6/7                      
## [215] Relict H                   L-imp melt                
## [217] CK3                        H3-an                     
## [219] Iron, IVB                  R3.8                      
## [221] L~5                        Mesosiderite-an           
## [223] Mesosiderite-A2            Pallasite, PES            
## [225] C4-ung                     Iron, IAB?                
## [227] Mesosiderite-A             R3.5-6                    
## [229] H3.9-6                     Ureilite-pmict            
## [231] LL~6                       CK4/5                     
## [233] EL4                        Lunar (feldsp. breccia)   
## [235] L3.9-6                     H-an                      
## [237] L/LL3-6                    L/LL3-5                   
## [239] H/L3.5                     H/L3                      
## [241] R3-4                       CK3-an                    
## [243] LL4-5                      H/L6                      
## [245] L3/4                       H-imp melt                
## [247] CR                         Chondrite-fusion crust    
## [249] Iron, IAB-sLH              H(L)3-an                  
## [251] L(LL)3                     H(L)3                     
## [253] R3                         L7                        
## [255] CM-an                      L/LL~6                    
## [257] L/LL~5                     L~4                       
## [259] L/LL~4                     LL(L)3                    
## [261] H3.2                       L-melt breccia            
## [263] H6-melt breccia            H5-melt breccia           
## [265] H-melt rock                Eucrite-an                
## [267] Lunar (bas/anor)           LL5/6                     
## [269] LL3/4                      H3.4/3.5                  
## [271] Lunar (basalt)             H/L5                      
## [273] H(5?)                      LL-imp melt               
## [275] Mesosiderite?              H~4/5                     
## [277] L6-melt breccia            L3.5-3.7                  
## [279] Iron, IIAB-an              L3.3-3.7                  
## [281] L3.2-3.6                   L3.3-3.6                  
## [283] Acapulcoite/Lodranite      Mesosiderite-B            
## [285] CK5/6                      L3.05                     
## [287] C2                         C4/5                      
## [289] L/LL3.2                    Iron, IIIAB?              
## [291] L3.5-5                     L/LL(?)3                  
## [293] H4(?)                      Iron, IAB-sHH             
## [295] Relict iron                EL4/5                     
## [297] L5-7                       Diogenite-an              
## [299] L-melt rock                CR1                       
## [301] E                          H-metal                   
## [303] L-metal                    Relict OC                 
## [305] EH                         Mesosiderite-A4           
## [307] L/LL5/6                    H3.8-4                    
## [309] CBb                        EL6/7                     
## [311] EL7                        CH/CBb                    
## [313] CO3.8                      H/L~4                     
## [315] Mesosiderite-C2            R5                        
## [317] H4/6                       H3.7-5                    
## [319] LL3.7-6                    H3.7/3.8                  
## [321] L3.7/3.8                   EH-imp melt               
## [323] R                          Fusion crust              
## [325] Aubrite-an                 R6                        
## [327] LL-melt rock               L3.5-3.9                  
## [329] L3.2-3.5                   L3.3-3.5                  
## [331] L3.0-3.7                   E3-an                     
## [333] K                          E3                        
## [335] Acapulcoite/lodranite      CK4-an                    
## [337] L(LL)3.05                  L3.10                     
## [339] CB                         Diogenite-olivine         
## [341] EL-melt rock               EH6                       
## [343] Pallasite, ungrouped       L/LL4/5                   
## [345] L3.8-an                    Iron, IAB-an              
## [347] C5/6-ung                   CV2                       
## [349] Iron, IC-an                Lunar (bas. breccia)      
## [351] L3.8-6                     R3/4                      
## [353] R3.9                       CK                        
## [355] LL3.10                     R4/5                      
## [357] L3.8-5                     Mesosiderite-C            
## [359] Enst achon                 H/L3-4                    
## [361] L(H)3                      LL6/7                     
## [363] LL3.1                      OC3                       
## [365] R3.7                       LL~4                      
## [367] LL~4/5                     L(LL)~4                   
## [369] H3.05                      H3.10                     
## [371] Impact melt breccia        LL3-5                     
## [373] H/L3.7                     LL3-4                     
## [375] CK3/4                      Martian                   
## [377] CO3.1                      Lunar (bas/gab brec)      
## [379] Achondrite-prim            LL<3.5                    
## [381] CK3.8                      L/LL-melt rock            
## [383] H6/7                       Iron, IAB-sHL-an          
## [385] CM2-an                     R3-5                      
## [387] L4-melt rock               L6-melt rock              
## [389] H/L4/5                     EL3/4                     
## [391] H/L6-melt rock             Enst achon-ung            
## [393] L3-7                       R3.4                      
## [395] LL3.05                     LL4/6                     
## [397] LL3.8-4                    H3.15                     
## [399] C3.0-ung                   LL-melt breccia           
## [401] LL6-melt breccia           L5-melt breccia           
## [403] LL(L)3.1                   LL6-an                    
## [405] L4-melt breccia            Howardite-an              
## [407] H4-melt breccia            Martian (basaltic breccia)
## [409] L3-melt breccia            L~4-6                     
## [411] LL~5                       R3.5-4                    
## [413] CR7                        H-melt breccia            
## [415] Lunar (norite)             L3.00                     
## [417] H3.0-3.4                   L/LL4-6                   
## [419] CM                         EH7                       
## [421] L4-an                      E-an                      
## [423] H3.8/3.9                   L3.9-5                    
## [425] H3.8-6                     H3.4-5                    
## [427] L3.0-3.9                   L3.5-3.8                  
## [429] H3.2-3.7                   L3.6-4                    
## [431] Iron, IIE?                 C3/4-ung                  
## [433] L/LL3.5                    L/LL3.6/3.7               
## [435] H/L4-5                     LL~3                      
## [437] Pallasite?                 LL5-7                     
## [439] LL3.9/4                    H3.8-an                   
## [441] CR-an                      L/LL5-6                   
## [443] L(LL)5                     L(LL)6                    
## [445] LL3.1-3.5                  E5                        
## [447] Lodranite-an               H3.2-6                    
## [449] H(?)4                      E5-an                     
## [451] H3.2-an                    EH6-an                    
## [453] Stone-ung                  C1/2-ung                  
## [455] L/LL                      
## 455 Levels: Acapulcoite Acapulcoite/lodranite ... Winonaite
stony_chondrites_carbonaceous <- c("CR2-an", "CI1", "CV3", "CM2", "OC", "C2-ung",
                                   "CO3.3", "CBa", "CR2", "CO3.2", "CK4", "CO3.5", 
                                   "CO3.6", "C3-ung", "CO3.4", "C", "CO3.7", "CH3",
                                   "CO3", "CM1/2", "CM1", "CO3.0", "CV3-an", "CK6",
                                   "CK5", "C6", "C4", "C1-ung", "CK3", "C4-ung",
                                   "CK4/5", "CK3-an", "CR", "CM-an", "CK5/6", "C2",
                                   "C4/5", "CR1", "Relict OC", "CBb", "CH/CBb", 
                                   "CO3.8", "CK4-an", "CB", "C5/6-ung", "CV2", 
                                   "CK", "OC3", "CK3/4", "CO3.1", "CK3.8", "CM2-an", 
                                   "C3.0-ung", "CR7", "CM", "C3/4-ung", "CR-an", "C1/2-ung")

stony_chondrites_ordinary <- c("L5", "H6", "L6", "LL3-6", "H5", "L", "H4", "H",
                               "LL5", "L/LL4", "L3", "LL6", "L4", "LL4", "LL3.8", 
                               "LL3.15", "LL3.9", "H/L3.9", "H4-an", "L/LL6", "H/L4",
                               "H4-5", "L3.7", "LL3.4", "H3.8", "H3-5", "H5-6", "H5-7",
                               "L3-6", "H4-6", "H3", "L/LL5", "H3.7", "H4/5", "H3/4", "H?", 
                               "H3-6", "L3.4", "L3.7-6", "L5/6", "L3.6", "LL3.2", "L3-4", 
                               "H5/6", "L5-6", "LL3.6", "H3-4", "LL", "L4-6", "LL3.00" ,
                               "H3.4", "LL3.3", "H/L3.6", "LL7", "LL4-6", "L/LL6-an", 
                               "H3.9/4", "L3.8", "LL5-6", "LL3.8-6", "L3.9", "L4-5", 
                               "L3-5", "LL4/5", "L4/5", "H3.9", "H3.6-6", "H3.8-5",
                               "H3.8/4", "H3.9-5", "L3.9/4", "H~5", "H~6", "L/LL3.10",
                               "LL3", "L~6", "L~3", "H~4", "L(LL)3.5-3.7", "H3.6", "L3.4-3.7",
                               "L3.5", "LL7(?)", "LL6(?)", "H3.5-4", "H3.5", "L/LL3",
                               "H7", "L(?)3", "L3.2", "L3.7-3.9", "LL3.7", "LL3.5",
                               "L3.7-4", "L3.3", "L3.1", "H3.3", "H3.7-6", "H3.0", "H3.1",
                               "L3.0", "L/LL3.4", "LL3.0", "H5-an", "L6/7", "Relict H", 
                               "L-imp melt", "H3-an", "L~5", "H3.9-6",  "LL~6", "L3.9-6",
                               "H-an", "L/LL3-6", "L/LL3-5", "H/L3.5", "H/L3", "LL4-5",
                               "H/L6", "L3/4", "H-imp melt", "H(L)3-an", "L(LL)3",
                               "H(L)3", "L7", "L/LL~6", "L/LL~5", "L~4", "L/LL~4",
                               "LL(L)3 ", "H3.2", "L-melt breccia", "H6-melt breccia",
                               "H5-melt breccia", "H-melt rock", "LL5/6", "LL3/4", 
                               "H3.4/3.5", "H/L5", "H(5?)", "LL-imp melt", "H~4/5",
                               "L6-melt breccia", "L3.5-3.7", "L3.3-3.7", "L3.2-3.6",
                               "L3.3-3.6", "L3.05",  "L/LL3.2", "L3.5-5", "L/LL(?)3", 
                               "H4(?)", "L5-7", "L-melt rock", "H-metal", "L-metal",
                               "L/LL5/6", "H3.8-4", "H/L~4",  "H4/6", "H3.7-5", "LL3.7-6",
                               "H3.7/3.8", "L3.7/3.8", "LL-melt rock", "L3.5-3.9", 
                               "L3.2-3.5", "L3.3-3.5", "L3.0-3.7", "L(LL)3.05", "L3.10",
                                "L/LL4/5", "L3.8-an", "L3.8-6", "LL3.10", "L3.8-5", 
                               "H/L3-4", "L(H)3", "LL6/7", "LL3.1", "LL~4", "LL~4/5", 
                               "L(LL)~4", "H3.05", "H3.10", "LL3-5", "H/L3.7", 
                               "LL3-4", "LL<3.5", "L/LL-melt rock", "H6/7", "L4-melt rock",
                               "L6-melt rock", "H/L4/5", "H/L6-melt rock", "L3-7", "LL3.05",
                               "LL4/6", "LL3.8-4", "H3.15", "LL-melt breccia", "LL6-melt breccia",
                               "L5-melt breccia", "LL(L)3.1", "LL6-an", "L4-melt breccia", 
                               "H4-melt breccia", "L3-melt breccia", "L~4-6", "LL~5", 
                               "H-melt breccia", "L3.00", "H3.0-3.4", "L/LL4-6", 
                               "L4-an", "H3.8/3.9", "L3.9-5", "H3.8-6", "H3.4-5", 
                               "L3.0-3.9", "L3.5-3.8", "H3.2-3.7", "L3.6-4", "L/LL3.5",
                               "L/LL3.6/3.7", "H/L4-5", "LL~3", "LL5-7", "LL3.9/4", "H3.8-an",
                               "L/LL5-6", "L(LL)5", "L(LL)6", "LL3.1-3.5", "H3.2-6", 
                               "H(?)4", "H3.2-an", "L/LL", "LL(L)3")

stony_chondrites_enstatite <- c("EH4", "EL6", "EH3/4-an", "EH7-an", "EH3",  "EH5",
                                "E4", "EL5", "EL3", "E6", "EH4/5", "EL4", "EL4/5",
                                "E", "EH", "EL6/7", "EL7", "EH-imp melt", "E3-an", "E3",
                                "EH6", "EL-melt rock", "EL3/4", "EH7", "E-an", "E5",
                                "E5-an", "EH6-an")

stony_chondrites <- c("Stone-uncl", "K3", "R3.8-6", "R3.8-5", "Chondrite-ung", "R3.6",
                      "R4", "R3-6", "R3.8", "R3.5-6", "R3-4", "Chondrite-fusion crust", 
                      "R3", "R5", "R", "Fusion crust", "R6", "K", "R3/4", "R3.9", "R4/5",
                      "R3.7", "R3-5", "R3.4", "R3.5-4")

stony_primitive_achondrites <- c("Acapulcoite", "Ureilite-an", "Ureilite",  "Lodranite",
                                 "Winonaite", "Brachinite",  "Ureilite-pmict",
                                 "Acapulcoite/Lodranite", "Acapulcoite/lodranite", 
                                 "Achondrite-prim", "Lodranite-an")

stony_achondrites <- c("Diogenite-pm", "Eucrite-mmict", "Angrite", "Aubrite", "Howardite",
                       "Eucrite-pmict", "Diogenite", "Eucrite", "Martian (chassignite)",
                       "Eucrite-cm", "Martian (nakhlite)", "Eucrite-br", "Martian (shergottite)",
                       "Achondrite-ung", "Martian (OPX)", "Eucrite-Mg rich", "Eucrite-unbr", 
                       "Lunar (anorth)", "Lunar", "Lunar (gabbro)", "Lunar (feldsp. breccia)",
                       "Eucrite-an", "Lunar (bas/anor)", "Lunar (basalt)", "Diogenite-an", 
                       "Aubrite-an", "Diogenite-olivine", "Lunar (bas. breccia)", "Enst achon",
                       "Impact melt breccia", "Martian", "Lunar (bas/gab brec)", "Enst achon-ung",
                       "Howardite-an", "Martian (basaltic breccia)", "Lunar (norite)", 
                       "Stone-ung")

stony_iron_achondrites <- c("Mesosiderite-A1", "Mesosiderite", "Mesosiderite-A3/4",
                            "Pallasite", "Mesosiderite-A3", "Pallasite, PMG", "Mesosiderite-B2",
                            "Pallasite, PMG-an", "Mesosiderite-B1", "Mesosiderite-B4",
                            "Mesosiderite-an", "Mesosiderite-A2", "Pallasite, PES",
                            "Mesosiderite-A", "Mesosiderite?",  "Mesosiderite-B",
                            "Mesosiderite-A4", "Mesosiderite-C2", "Pallasite, ungrouped",
                            "Mesosiderite-C", "Pallasite?")

iron_achondrites <- c("Iron, IVA", "Iron, IIAB", "Iron, IAB-sLL", "Iron, ungrouped",
                      "Iron, IAB-MG", "Iron?", "Iron, IIIAB", "Iron, IID", "Iron, IIE",
                      "Iron, IAB-sHL", "Iron", "Iron, IIE-an", "Iron, IAB-ung", "Iron, IIF",
                      "Iron, IIIAB-an", "Iron, IAB complex", "Iron, IIIE-an",  "Iron, IAB-sLM",
                      "Iron, IC", "Iron, IID-an", "Iron, IIIE", "Iron, IVA-an", "Iron, IIG",
                      "Iron, IIC", "Iron, IIIF", "Iron, IVB", "Iron, IAB?", "Iron, IAB-sLH",
                      "Iron, IIAB-an", "Iron, IIIAB?", "Iron, IAB-sHH", "Relict iron", 
                      "Iron, IAB-an", "Iron, IC-an", "Iron, IAB-sHL-an", "Iron, IIE?")

other_unknown <- c("Unknown")


meteorites_class <- fct_collapse(meteorites$class,
             scc = stony_chondrites_carbonaceous,
             sco = stony_chondrites_ordinary,
             sce = stony_chondrites_enstatite,
             sc = stony_chondrites,
             spa = stony_primitive_achondrites,
             sa = stony_achondrites,
             sia = stony_iron_achondrites,
             ia = iron_achondrites,
             ou = other_unknown)
## Warning: Unknown levels in `f`: LL(L)3
meteorites$class <- as.factor(meteorites_class)

levels(meteorites_class)
## [1] "spa" "sa"  "scc" "sc"  "sce" "sco" "ia"  "sia" "ou"
labels <- c("Stony Primitive Achondrites",
            "Stony Achondrites",
            "Stony Chondrites - Carbonaceous",
            "Stony Chondrites",
            "Stony Chondrites - Enstatite",
            "Stony Chondrites - Ordinary",
            "Iron Achondrites",
            "Stony Iron Achondrites",
            "Other / Unknown")

# create bins for color palette
bins <- c(0, 20, 40, 60, 80, 120, 150, Inf)
# create palette
palette <- colorFactor("Paired", domain = meteorites$class)
    
mytext <- paste(meteorites$name,
                    "<br/>",
                    meteorites$year,
                    "<br/>",
                    "Size (kg): ", (meteorites$mass)/1000,
                    "<br/>",
                    sep="") %>%
      lapply(htmltools::HTML)
    
leaflet(meteorites) %>% 
  addProviderTiles(providers$CartoDB.DarkMatter) %>%
  setView(lat = 20, lng = 0, zoom = 1.6) %>%
  addCircleMarkers(~long, ~lat, # Points for circles
             fillColor = ~palette(class), 
             fillOpacity = 0.7, # make opaque
             color = "white",  # colour of edge?
             radius = ~log(mass), # radius
             stroke = FALSE, # no edge on circles
             weight = 1,# hmm edge of lines?
             clusterOptions = markerClusterOptions(
               disableClusteringAtZoom = 4,
               spiderfyOnMaxZoom = F
             ),
    label = mytext, 
    labelOptions = labelOptions(style = list("font-weight" = "normal",
                                             padding = "3px 8px"), 
                                textsize = "13px", 
                                direction = "auto")) %>%
  addLegend(position = "bottomright",
            pal = palette,
            values = ~class,
            title = "Class of Meteorite",
            opacity = 1,
            labFormat = function(type, cuts, p) {  # Here's the trick
                                                  paste0(labels)
                             }
                             ) 
## Warning in validateCoords(lng, lat, funcName): Data contains 7315 rows with
## either missing or invalid lat/lon values and will be ignored

Sankey diagram

library(networkD3)
## 
## Attaching package: 'networkD3'
## The following object is masked from 'package:leaflet':
## 
##     JS
library(devtools)
#install_github("Displayr/flipPlots") #install from github
library(flipPlots)
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following object is masked from 'package:maps':
## 
##     ozone
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
library(dplyr)

# remove zero values
zero_values <- which(meteorites$mass == 0)
meteorites_all <- meteorites[-zero_values, ]
# remove NAs for mass
meteorites_all <- meteorites %>%
  na.omit()

# how many meteorites have fallen?
fallen <- meteorites_all %>%
  filter(fall == "Fell") %>%
  dplyr::summarise(Count = n())

# how many meteorites have been found?
found <- meteorites_all %>%
  filter(fall == "Found") %>%
  dplyr::summarise(Count = n())

summary(meteorites_all$mass)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
##        0        7       29    15601      187 60000000
hist(meteorites_all$mass, breaks = 10, freq = T)

# how many meteorites in each class
meteorites_all_class <- fct_collapse(meteorites_all$class,
             scc = stony_chondrites_carbonaceous,
             sco = stony_chondrites_ordinary,
             sce = stony_chondrites_enstatite,
             sc = stony_chondrites,
             spa = stony_primitive_achondrites,
             sa = stony_achondrites,
             sia = stony_iron_achondrites,
             ia = iron_achondrites,
             ou = other_unknown)
## Warning: Unknown levels in `f`: CR2-an, CI1, CV3, CM2, OC, C2-ung, CO3.3, CBa,
## CR2, CO3.2, CK4, CO3.5, CO3.6, C3-ung, CO3.4, C, CO3.7, CH3, CO3, CM1/2, CM1,
## CO3.0, CV3-an, CK6, CK5, C6, C4, C1-ung, CK3, C4-ung, CK4/5, CK3-an, CR, CM-
## an, CK5/6, C2, C4/5, CR1, Relict OC, CBb, CH/CBb, CO3.8, CK4-an, CB, C5/6-ung,
## CV2, CK, OC3, CK3/4, CO3.1, CK3.8, CM2-an, C3.0-ung, CR7, CM, C3/4-ung, CR-
## an, C1/2-ung, L5, H6, L6, LL3-6, H5, L, H4, H, LL5, L/LL4, L3, LL6, L4, LL4,
## LL3.8, LL3.15, LL3.9, H/L3.9, H4-an, L/LL6, H/L4, H4-5, L3.7, LL3.4, H3.8, H3-5,
## H5-6, H5-7, L3-6, H4-6, H3, L/LL5, H3.7, H4/5, H3/4, H?, H3-6, L3.4, L3.7-6,
## L5/6, L3.6, LL3.2, L3-4, H5/6, L5-6, LL3.6, H3-4, LL, L4-6, LL3.00, H3.4,
## LL3.3, H/L3.6, LL7, LL4-6, L/LL6-an, H3.9/4, L3.8, LL5-6, LL3.8-6, L3.9, L4-5,
## L3-5, LL4/5, L4/5, H3.9, H3.6-6, H3.8-5, H3.8/4, H3.9-5, L3.9/4, H~5, H~6, L/
## LL3.10, LL3, L~6, L~3, H~4, L(LL)3.5-3.7, H3.6, L3.4-3.7, L3.5, LL7(?), LL6(?),
## H3.5-4, H3.5, L/LL3, H7, L(?)3, L3.2, L3.7-3.9, LL3.7, LL3.5, L3.7-4, L3.3,
## L3.1, H3.3, H3.7-6, H3.0, H3.1, L3.0, L/LL3.4, LL3.0, H5-an, L6/7, Relict H, L-
## imp melt, H3-an, L~5, H3.9-6, LL~6, L3.9-6, H-an, L/LL3-6, L/LL3-5, H/L3.5, H/
## L3, LL4-5, H/L6, L3/4, H-imp melt, H(L)3-an, L(LL)3, H(L)3, L7, L/LL~6, L/LL~5,
## L~4, L/LL~4, LL(L)3 , H3.2, L-melt breccia, H6-melt breccia, H5-melt breccia,
## H-melt rock, LL5/6, LL3/4, H3.4/3.5, H/L5, H(5?), LL-imp melt, H~4/5, L6-melt
## breccia, L3.5-3.7, L3.3-3.7, L3.2-3.6, L3.3-3.6, L3.05, L/LL3.2, L3.5-5, L/
## LL(?)3, H4(?), L5-7, L-melt rock, H-metal, L-metal, L/LL5/6, H3.8-4, H/L~4,
## H4/6, H3.7-5, LL3.7-6, H3.7/3.8, L3.7/3.8, LL-melt rock, L3.5-3.9, L3.2-3.5,
## L3.3-3.5, L3.0-3.7, L(LL)3.05, L3.10, L/LL4/5, L3.8-an, L3.8-6, LL3.10, L3.8-5,
## H/L3-4, L(H)3, LL6/7, LL3.1, LL~4, LL~4/5, L(LL)~4, H3.05, H3.10, LL3-5, H/
## L3.7, LL3-4, LL<3.5, L/LL-melt rock, H6/7, L4-melt rock, L6-melt rock, H/L4/5,
## H/L6-melt rock, L3-7, LL3.05, LL4/6, LL3.8-4, H3.15, LL-melt breccia, LL6-melt
## breccia, L5-melt breccia, LL(L)3.1, LL6-an, L4-melt breccia, H4-melt breccia,
## L3-melt breccia, L~4-6, LL~5, H-melt breccia, L3.00, H3.0-3.4, L/LL4-6, L4-
## an, H3.8/3.9, L3.9-5, H3.8-6, H3.4-5, L3.0-3.9, L3.5-3.8, H3.2-3.7, L3.6-4, L/
## LL3.5, L/LL3.6/3.7, H/L4-5, LL~3, LL5-7, LL3.9/4, H3.8-an, L/LL5-6, L(LL)5,
## L(LL)6, LL3.1-3.5, H3.2-6, H(?)4, H3.2-an, L/LL, LL(L)3, EH4, EL6, EH3/4-an,
## EH7-an, EH3, EH5, E4, EL5, EL3, E6, EH4/5, EL4, EL4/5, E, EH, EL6/7, EL7, EH-
## imp melt, E3-an, E3, EH6, EL-melt rock, EL3/4, EH7, E-an, E5, E5-an, EH6-an,
## Stone-uncl, K3, R3.8-6, R3.8-5, Chondrite-ung, R3.6, R4, R3-6, R3.8, R3.5-6,
## R3-4, Chondrite-fusion crust, R3, R5, R, Fusion crust, R6, K, R3/4, R3.9,
## R4/5, R3.7, R3-5, R3.4, R3.5-4, Acapulcoite, Ureilite-an, Ureilite, Lodranite,
## Winonaite, Brachinite, Ureilite-pmict, Acapulcoite/Lodranite, Acapulcoite/
## lodranite, Achondrite-prim, Lodranite-an, Diogenite-pm, Eucrite-mmict, Angrite,
## Aubrite, Howardite, Eucrite-pmict, Diogenite, Eucrite, Martian (chassignite),
## Eucrite-cm, Martian (nakhlite), Eucrite-br, Martian (shergottite), Achondrite-
## ung, Martian (OPX), Eucrite-Mg rich, Eucrite-unbr, Lunar (anorth), Lunar,
## Lunar (gabbro), Lunar (feldsp. breccia), Eucrite-an, Lunar (bas/anor), Lunar
## (basalt), Diogenite-an, Aubrite-an, Diogenite-olivine, Lunar (bas. breccia),
## Enst achon, Impact melt breccia, Martian, Lunar (bas/gab brec), Enst achon-
## ung, Howardite-an, Martian (basaltic breccia), Lunar (norite), Stone-ung,
## Mesosiderite-A1, Mesosiderite, Mesosiderite-A3/4, Pallasite, Mesosiderite-
## A3, Pallasite, PMG, Mesosiderite-B2, Pallasite, PMG-an, Mesosiderite-B1,
## Mesosiderite-B4, Mesosiderite-an, Mesosiderite-A2, Pallasite, PES, Mesosiderite-
## A, Mesosiderite?, Mesosiderite-B, Mesosiderite-A4, Mesosiderite-C2, Pallasite,
## ungrouped, Mesosiderite-C, Pallasite?, Iron, IVA, Iron, IIAB, Iron, IAB-sLL,
## Iron, ungrouped, Iron, IAB-MG, Iron?, Iron, IIIAB, Iron, IID, Iron, IIE, Iron,
## IAB-sHL, Iron, Iron, IIE-an, Iron, IAB-ung, Iron, IIF, Iron, IIIAB-an, Iron, IAB
## complex, Iron, IIIE-an, Iron, IAB-sLM, Iron, IC, Iron, IID-an, Iron, IIIE, Iron,
## IVA-an, Iron, IIG, Iron, IIC, Iron, IIIF, Iron, IVB, Iron, IAB?, Iron, IAB-sLH,
## Iron, IIAB-an, Iron, IIIAB?, Iron, IAB-sHH, Relict iron, Iron, IAB-an, Iron, IC-
## an, Iron, IAB-sHL-an, Iron, IIE?, Unknown
meteorites_all$class <- as.factor(meteorites_all_class)

levels(meteorites_all$class)
## [1] "spa" "sa"  "scc" "sc"  "sce" "sco" "ia"  "sia" "ou"
meteorites_all$class <- revalue(meteorites_all$class,
        c("scc" = "Stony Chondrites - Carbonaceous",
          "sco" = "Stony Chondrites - Ordinary",
          "sce" = "Stony Chondrites - Enstatite",
          "sc" = "Stony Chondrites",
          "spa" = "Stony Primitive Achondrites",
          "sa" = "Stony Achondrites",
          "sia" = "Stony-Iron Achondrites",
          "ia" = "Iron Achondrites",
          "ou" = "Other / Unknown"))

meteorites_all$fall <- as.factor(meteorites_all$fall)

meteorites_all$masscat <- cut(meteorites_all$mass, c(-1, 49, 99, 999, 9999, Inf),
                              labels = c("0-49g", "50-99g", "100g-999g", "1kg-9.999kg", "10kg-60 tonnes"))

classes <- meteorites_all %>%
  group_by(fall, masscat, class) %>%
  dplyr::summarise(Count = n())
## `summarise()` has grouped output by 'fall', 'masscat'. You can override using the `.groups` argument.
colourpalette <- c("grey80", "grey60",
                   "#AED6F1", "#5DADE2", "#3498DB", "#2E86C1", "#2874A6",
                   "red", "purple", "orange", "pink", "green", "darkgreen", "yellow", "coral")

SankeyDiagram(classes[ ,-4],
              link.color = "Source",
              weights = classes$Count, 
              label.show.counts = TRUE,
              label.show.varname = F,
              font.family = "Avenir",
              hovertext.show.percentages = T,
              sinks.right = F,
              colors = colourpalette)